package com.example.job_backend.controller;

import com.example.job_backend.model.Query.ResumeTemplateQuery;
import com.example.job_backend.model.Query.ResumeTemplatesQuery;
import com.example.job_backend.model.R;
import com.example.job_backend.model.ResumeTemplate;
import com.example.job_backend.service.ResumeTemplateService;
import com.example.job_backend.util.AliOSSUtils;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class ResumeAdminController {
    @Resource
    private ResumeTemplateService resumeTemplateService;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @GetMapping("/resumes/templates")
    public R findAllResumeTemplate(ResumeTemplateQuery query) {
        PageInfo<ResumeTemplate> list = resumeTemplateService.getResumeTemplateByPage(query);
        return new R(200, "请求成功", list);
    }

    //创建简历时用的
    @GetMapping("/resumes/all")
    public R getAllTemplate() {
        List<ResumeTemplate> list = resumeTemplateService.getAllTemplate();
        return new R(200, "请求成功", list);
    }

    @PostMapping("/resumes/uploadTemplate")
    public R uploadTemplate(MultipartFile file) throws IOException {
        String url = aliOSSUtils.uploadFile(file);
        return new R(200, "上传成功", url);
    }

    @GetMapping("/resumes/addTemplate")
    public R addTemplate(ResumeTemplate resumeTemplate) {
        resumeTemplateService.save(resumeTemplate);
        return new R(200, "添加成功", null);
    }

    @GetMapping("/deleteTemplate/{id}")
    public R deleteTemplate(@PathVariable("id") Integer id) {
        resumeTemplateService.removeById(id);
        return new R(200, "删除成功", null);
    }

    @GetMapping("/resumes/updateTemplate")
    public R updateTemplate(ResumeTemplate resumeTemplate) {
        resumeTemplateService.updateById(resumeTemplate);
        return new R(200, "更新成功", null);
    }
}
